package com.example.studentmanager.Controller;

import com.example.studentmanager.Pojo.Student;
import com.example.studentmanager.Service.impl.Studentimpl;


import java.io.*;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "main", value = "/main")
public  class loginController extends  HttpServlet{
    private static  String  admin="admin";
    private  static String password="123456";
    //给空了 直接进去
    private  static  String  error="您账号或者密码有误，此系统仅限管理员使用，请联系管理员QQ656277239";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    res.setCharacterEncoding("utf-8");
    req.setCharacterEncoding("utf-8");

    String requser=req.getParameter("admin");
    String password1=req.getParameter("password");
 HttpSession ss= req.getSession();

if (admin.equals(ss.getAttribute("user"))&&password.equals(ss.getAttribute("password"))){
    try{
        List<Student> list=new Studentimpl().queryALL();
        req.setAttribute("list",list);
        RequestDispatcher ds= req.getRequestDispatcher("./homepage.jsp");
        ds.forward(req,res);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

}else {

    if(admin.equals(requser)&& password.equals(password1)){
        System.out.println("管理员登录成功");
        HttpSession session=req.getSession();
        session.setAttribute("user",admin);
        session.setAttribute("password",password);
        try {
            List<Student> list=new Studentimpl().queryALL();
            req.setAttribute("list",list);
            RequestDispatcher ds= req.getRequestDispatcher("./homepage.jsp");
            ds.forward(req,res);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }else {
        System.out.println("登录失败");
        RequestDispatcher ds= req.getRequestDispatcher("./404.jsp");
        req.setAttribute("error",error);
        ds.forward(req,res);
    }
}}
}